package com.example.demo.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 自定义用户登录的拦截器
 */
@Component
public class LoginIntercept implements HandlerInterceptor {
    /**
     * 返回true表示拦截判断通过，可以访问后面的接口
     * 返回false表示拦截未通过，直接返回结果给前端
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        //1、得到HttpSession对象
        HttpSession session = request.getSession(false);
        if (session != null && session.getAttribute("userinfo") != null){
            //表示已经登录
            return true;
        }
        //执行到此行代码表示未登录，未登录就跳转到登录界面
        response.sendRedirect("/login.html");
        return false;
    }
}
